/*
 *  Copyright (c) 2008 Howard Shank
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
 * Software, and to permit persons to whom the Software is furnished to do so, subject 
 * to the following conditions:

 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

// README

Type.registerNamespace('AjaxCtrlsExtenders');

AjaxCtrlsExtenders.AutoFocusBehavior = function(element) {

    AjaxCtrlsExtenders.AutoFocusBehavior.initializeBase(this, [element]);

    this._debug = false;
    this._RestoreOnMouseout = null;
    this._RestoreControlID = null;

    this._mouseoverDelegate = null;
    this._mouseoutDelegate = null;
}

AjaxCtrlsExtenders.AutoFocusBehavior.prototype = {
    debug : function(str) {
        if(this._debug)
        {
            alert(str);
        }
    },
    
    initialize : function() {
        AjaxCtrlsExtenders.AutoFocusBehavior.callBaseMethod(this, 'initialize');
        
        var element = this.get_element();
        if(!element)
        {
            this.debug('element is null');
        }
        
        if(this._mouseoverDelegate == null)
        {
            this._mouseoverDelegate = Function.createDelegate(this, this._onMouseoverHandler);
        }
        if(this._mouseoverDelegate)
        {
            Sys.UI.DomEvent.addHandler(element, 'mouseover', this._mouseoverDelegate);
        }
        else
        {
            this.debug('this._mouseoverDelegate is null');
        }

        if(this._mouseoutDelegate == null)
        {
            this._mouseoutDelegate = Function.createDelegate(this, this._onMouseoutHandler);
        }
        if(this._mouseoutDelegate)
        {
            Sys.UI.DomEvent.addHandler(element, 'mouseout', this._mouseoutDelegate);
        }
        else
        {
            this.debug('this._mouseoverDelegate is null');
        }
    },

    dispose : function() {
        var element = this.get_element();
        if(this._mouseoverDelegate)
        {
            Sys.UI.DomEvent.removeHandler(element, 'mouseover', this._mouseoverDelegate);
            delete this._mouseoverDelegate;
        }
        if(this._mouseoutDelegate)
        {
            Sys.UI.DomEvent.removeHandler(element, 'mouseout', this._mouseoutDelegate);
            delete this._mouseoutDelegate;
        }
        AjaxCtrlsExtenders.AutoFocusBehavior.callBaseMethod(this, 'dispose');   
    },

    get_RestoreOnMouseout : function() {
        return this._RestoreOnMouseout;
    },

    set_RestoreOnMouseout : function(value) {
        if(this._RestoreOnMouseout != value)
        {
            this._RestoreOnMouseout = value;
            this.raisePropertyChanged('RestoreOnMouseout');
        }
    },
    
    get_RestoreControlID : function() {
        return this._RestoreControlID;
    },

    set_RestoreControlID : function(value) {
        if(this._RestoreControlID != value)
        {
            this._RestoreControlID = value;
            this.raisePropertyChanged('RestoreControlID');
        }
    },
    
    // TODO: (Step 3) Add functions here
    _onMouseoverHandler : function(e) {
        /// <summary>
        /// Control's mouseover handler to set focus to that control
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">
        /// Event info
        /// </param>
        this.debug('_onMouseoverHandler');
        if (this.get_element() && !this.get_element().disabled)
        {
            this._RestoreControlID = document.activeElement.id;
            this.get_element().focus(); // set focus
        }
    },
    _onMouseoutHandler : function(e) {
        /// <summary>
        /// Control's mouseout handler to restore focus if necessary
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">
        /// Event info
        /// </param>
        this.debug('_onMouseoutHandler');
        if (this.get_element() && !this.get_element().disabled)
        {
            if( this._RestoreOnMouseout && this._RestoreControlID != null && this._RestoreControlID != "" )
            {
                var element = $get(this._RestoreControlID);
                element.focus(); // set focus
            }
        }
    }
}

AjaxCtrlsExtenders.AutoFocusBehavior.registerClass('AjaxCtrlsExtenders.AutoFocusBehavior', AjaxControlToolkit.BehaviorBase);
